<%@ page import="vy.org.app.common.*" %>
<%@ page import="org.vy.dal.dao.*" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sf.json.JSON" %>
<%@ page import="net.sf.json.JSONSerializer" %>
<%@ page import="java.util.Map.Entry" %>
<%@ page import="vy.org.app.page.printlabel.report.instance.*" %>
<%@ taglib uri="/WEB-INF/lib/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/lib/struts-bean.tld" prefix="bean"%>

<%!
	private static final String EMPTY = "&nbsp;";
	private UserCtx m_user;
%>

<%
	ResourceBundle RESOURCE = ResourceBundle.getBundle("LabelResource",ActionCtx.ctx().getLocale());
	m_user = ActionCtx.ctx(request).getUser();
	MntnPrintLabelReportInstanceForm mntnPrintLabelReportInstanceForm = (MntnPrintLabelReportInstanceForm)request.getAttribute("mntnPrintLabelReportInstanceForm");
	if(mntnPrintLabelReportInstanceForm == null)
		mntnPrintLabelReportInstanceForm = new MntnPrintLabelReportInstanceForm();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
	<title>Label report</title>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8"/>
	<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"/> 
	<link rel="stylesheet" type="text/css" href="./css/Common.css" />
	<script type="text/javascript" src="./scripts/Common.js"></script>
	<style>
		.rgt {text-align:right;padding:3px 5px;}
		.secv {display:none;}
		.rpt td {background-color:#EBEBEB}
		.adj-div {float:left;margin-right:15px;}
	</style>
</head> 

<body>

 <form name="mntnPrintLabelReportInstanceForm" action="./runPrintLabelReportInsance.do" method="post">
	<input type="hidden" id="reportType" name="reportType" value="<%=mntnPrintLabelReportInstanceForm.getReportType()!=null?mntnPrintLabelReportInstanceForm.getReportType() : "0" %>">
	<input type="hidden" id="reportId" name="reportId"  value="<%=mntnPrintLabelReportInstanceForm.getReportId()!=null?mntnPrintLabelReportInstanceForm.getReportId() : "0" %>">
	<input type="hidden" id="ops" name="ops" value="RUN">
	<table cellspacing="0" cellpadding="2" border="1" class="rpt" width="70%">
		<tr>
			<td>Report</td>
			<td>
				<select name="report" id="report" onchange="changeReport()">
					<option value="-1">Select</option>
				  	<%
				  		List<VyPrintLabelReportDO> reportList = mntnPrintLabelReportInstanceForm.getReports();
				  		
				  		if(reportList != null){
					  		for(VyPrintLabelReportDO rpt : reportList){
					  			if(rpt.getReportId().equals(mntnPrintLabelReportInstanceForm.getReportId()))
						  			out.print("<option selected=\"selected\" value=\""+ rpt.getReportId()+"~"+rpt.getReportType() +"\">"+ rpt.getReportName() +"</option>");
					  			else
						  			out.print("<option value=\""+ rpt.getReportId()+"~"+rpt.getReportType() +"\">"+ rpt.getReportName() +"</option>");
					  		}
				  		}
				  	%>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="rgt">&nbsp;
			</td>
		</tr>
	</table>
  <div class="rst-panel" style="width:auto;">  
  <table class="rst" cellpadding="0" cellspacing="0" width="90%" border="0">
  	<thead>
  		<tr>
  			<th><div class="hd">Report Instance</div></th>
  			<th><div class="hd">Status</div></th>
  			<th><div class="hd">Start time</div></th>
  			<th><div class="hd">End time</div></th>
  			<th><div class="hd"><%=EMPTY %></div></th>
  		</tr>
  	</thead>
  	<tbody>
  <%
    boolean isRunning = false;
  	if(mntnPrintLabelReportInstanceForm.getInstances() != null){
  		List<VyPrintLabelReportInstanceDO> results = mntnPrintLabelReportInstanceForm.getInstances();
  		int idx=0;
  		for(VyPrintLabelReportInstanceDO rec : results){
  			if((rec.getStatus() == ReportStatusEnum.SUCCESS.getId()
  					|| rec.getStatus() == ReportStatusEnum.CANCELLED.getId()) && !isRunning)
  				isRunning = true;
  			else
  				isRunning = false;
  			%><tr>
			<td><%=rec.getReportInstanceId()%></td>
			<td><%=ReportStatusEnum.get(rec.getStatus(), ReportStatusEnum.class).getName()%></td>
			<td><%=rec.getStartTime() %></td>
			<td><%=rec.getEndTime()!=null?rec.getEndTime():EMPTY %></td>
			<% if(rec.getStatus() == ReportStatusEnum.SUCCESS.getId()) {%>
			<td><a target="_blank" href="./runPrintLabelReportInsance.do?ops=DOWNLOAD&reportInstanceId=<%=rec.getReportInstanceId()%>">Download</a></td>
			<% } else { %>
			<td><%=EMPTY %></td>
			<% } %>
			</tr>
			<%
  		}
  	}else{
  		%><tr><td colspan="5"><%=RESOURCE.getString("NO_RECORDS") %></td></tr><%
  	}
  %>
  </tbody>
  </table>
 </div>
 <% if(mntnPrintLabelReportInstanceForm.getReportId() != null) {%>
		<input type="submit" name="submitbtn" value="Run" />
 <% } %>
</form>

<script>

function changeReport(){
	var sel = document.getElementById("report");
	var val = sel.value;
	if(val != "-1"){
		var arr = val.split("~");		
		document.getElementById("reportId").value = arr[0];
		document.getElementById("reportType").value = arr[1];
		document.getElementById("ops").value = "SHOW";
	}
	document.mntnPrintLabelReportInstanceForm.submit();
}

</script>
</body>
</html>
